#include <sys/timerfd.h>
#include <unistd.h>
#include <cstring>
#include <iostream>

int main()
{
    int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (timerfd < 0) {
        std::cerr << "timerfd_create failed" << std::endl;
        return 1;
    }
    struct itimerspec itimerspec;
    bzero(&itimerspec, sizeof(itimerspec));
    itimerspec.it_value.tv_sec = 1;
    itimerspec.it_value.tv_nsec = 0;
    itimerspec.it_interval.tv_sec = 1;
    itimerspec.it_interval.tv_nsec = 0;
    timerfd_settime(timerfd, 0, &itimerspec, nullptr);

    uint64_t exp;
    while (true)
    {
        if (read(timerfd, &exp, 8) < 0) {
            std::cerr << "read err" << std::endl;
        }
        std::cout << "超时" << exp << "次..." << std::endl;
    }
    return 0;
}